//
//  UITableViewCell+BTSectionCircular.m
//  BTShike2
//
//  Created by 刘军林 on 2019/10/17.
//  Copyright © 2019 刘军林. All rights reserved.
//

#import "UITableViewCell+Circular.h"

@implementation UITableViewCell (Circular)

-(void) setCircularTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath cornerRadius:(CGFloat)cornerRadius leftRightSpace:(CGFloat)leftRightSpace topSpace:(CGFloat)topSpace
{
    self.backgroundColor = [UIColor clearColor];
    
    for (UIView *v in self.contentView.subviews) {
        if (v.tag == 999) {
            [v removeFromSuperview];
        }
    }
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(leftRightSpace, topSpace, tableView.frame.size.width - leftRightSpace - leftRightSpace, self.contentView.frame.size.height-topSpace)];
    bgView.tag = 999;
    bgView.backgroundColor = [UIColor clearColor];
    [self.contentView insertSubview:bgView atIndex:0];
    UIBezierPath *path = [UIBezierPath bezierPath];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.masksToBounds = YES;
    maskLayer.frame = bgView.bounds;
    bgView.layer.mask = maskLayer;
    maskLayer.fillColor = [UIColor whiteColor].CGColor;
    path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds cornerRadius:cornerRadius];

    maskLayer.path = path.CGPath;
    [bgView.layer addSublayer:maskLayer];
}

@end
